export default {
	data() {
		return {
			problem: '',
			answer: '',
			show: false,
			key: '',
			chat: [],
			type: ''
		}
	},
	onShow() {
		this.chat = [];
		this.init()

	},
	methods: {
		init() {
			// this.chat = uni.getStorageSync('historyProblem') || [];
			this.key = uni.getStorageSync('key') || ''
		},
		copy(val) {
			uni.setClipboardData({
				data: this.answer || val,
				success: function() {
					uni.showToast({
						title: '复制成功',
						icon: 'none'
					})
				}
			});
		},
		changeKey() {
			uni.setStorageSync('key', this.key)
			this.show = false
		},
		// 下个问题
		next() {
			uni.removeStorageSync('historyProblem');
			this.init()
			uni.showToast({
				title: '记忆已经清除！'
			})
		},
		async getAnswer() {
			console.log(this.user_find_num);
			let index = this.chat.length
			// if(this.user_find_num == 0) {
			// 	uni.showToast({
			// 		title: '你查询次数已上限',
			// 		icon: 'none'
			// 	})
			// 	return
			// }
			if (this.problem == '清除记忆') {
				this.next()
				return
			}
			// if (!this.problem) {
			// 	uni.showToast({
			// 		title: '你还没有输入问题呢！',
			// 		icon: 'none'
			// 	})
			// 	return
			// }

			try {
				let query = ''
				this.chat.forEach(item => {
					query += `\nQ: ${item.problem}\n\n\nA: ${item.answer}。 <|endoftext|>\n`
				})
				query += `Q: ${this.problem} + "\nA: `

				if (this.type == 'chat') {
					this.chat.push({
						problem: this.problem,
						answer: ''
					})
					uni.pageScrollTo({
						selector: '.seize',
					})
					this.problem = ''
				} else {
					uni.showLoading({
						title: '拼命思考中...',
					})
				}
				console.log('3', this.chat);
				// const {
				// 	data
				// } = await uni.$u.http.post('http://chatgpt.com/api/index', {
				// 	"prompt": query,
				// 	"key": uni.getStorageSync('key') || ''
				// }, {
				// 	timeout: 300000
				// })
				console.log('t', this.problem);
				uni.request({
					url: 'http://chatadmin.baimajingxuan.com/api/index',
					data: { //参数
						message: this.chat[this.chat.length - 1].problem,
						modelType: this.imgType
					},
					header: {
						token: uni.getStorageSync('token')
					},
					method: 'POST', //请求方式，必须为大写
					success: (data) => {
						console.log('接口返回------', data);
						uni.hideLoading()
						if (data.data.code == 0) {
							console.log('接口返回------', data.data.msg);
							uni.showToast({
								title: data.data.msg,
								icon: 'none'
							})
							this.$set(this.chat, index, {
								...this.chat[index],
								answer: 'error'
							})
							return
						}
						//绘画
						if(this.imgType == 7) {
							console.log('data.data.data',data.data.data);
							this.answer = data.data.data.data[0].url
						}else {
							this.answer = data.data.data.choices[0].text
						}
						if (!this.answer) return
						uni.pageScrollTo({
							selector: '.seize',
						})
						this.$set(this.chat, index, {
							...this.chat[index],
							answer: this.answer
						})
						uni.setStorageSync('historyProblem', this.chat)
						this.init()
					}
				});
				return;
			} catch (e) {
				uni.hideLoading()
				if (e.statusCode == 401) {
					uni.showToast({
						title: 'key无效',
						icon: 'none'
					})
					return
				}
				uni.showToast({
					title: '获取失败，请联系管理员' + e,
					icon: 'none'
				})
				this.$set(this.chat, index, {
					...this.chat[index],
					answer: 'error'
				})
				uni.pageScrollTo({
					selector: '.seize',
				})
			}

		}

	}
}
